﻿Imports System.Data.SqlClient
Public Class clsZONA
    Private mstrID_ZONA As String
    Private mstrCOD_ZONA As String
    Public Property ID_ZONA() As String
        Get
            Return mstrID_ZONA
        End Get
        Set(ByVal value As String)
            mstrID_ZONA = value
        End Set
    End Property
    Public Property COD_ZONA() As String
        Get
            Return mstrCOD_ZONA
        End Get
        Set(ByVal value As String)
            mstrCOD_ZONA = value
        End Set
    End Property
    Public Sub New()
        mstrID_ZONA = ""
        mstrCOD_ZONA = ""
    End Sub
    Public Sub Insertar()
        Dim sql As String = "Insert Into ZONA (" & _
                            "ID_ZONA," & _
                            "COD_ZONA)" & _
                            "VALUES (" & _
                            "SUBSTRING(@ID_ZONA,1,100)," & _
                            "SUBSTRING(@COD_ZONA,1,100))"

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = OptocontrolConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("ID_ZONA", mstrID_ZONA)
        Command.Parameters.AddWithValue("COD_ZONA", mstrCOD_ZONA)

        cnConn.Open()
        Dim RowsAffected As Integer
        Try
            RowsAffected = Command.ExecuteNonQuery
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")
        End Try

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible insertar el registro de zona", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
    Public Sub Actualizar()

        Dim strSqlQry As String
        Dim cnConn As New SqlConnection

        cnConn.ConnectionString = OptocontrolConnection
        strSqlQry = "Update ZONA Set " & _
                    "COD_ZONA=SUBSTRING(@COD_ZONA,1,100) " & _
                    "Where ID_ZONA=@ID_ZONA"


        Dim Command As New SqlCommand(strSqlQry, cnConn)

        Command.Parameters.AddWithValue("ID_ZONA", mstrID_ZONA)
        Command.Parameters.AddWithValue("COD_ZONA", mstrCOD_ZONA)
        cnConn.Open()

        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible actualizar el zona ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If

    End Sub
End Class
